package com.jade.linkong.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jade.common.constant.Constants;
import com.jade.common.constant.UserConstants;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jade.common.annotation.Log;
import com.jade.common.enums.BusinessType;
import com.jade.linkong.domain.LkCompany;
import com.jade.linkong.service.ILkCompanyService;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.common.core.page.TableDataInfo;

/**
 * 企业Controller
 * 
 * @author jade
 * @date 2020-07-13
 */
@Controller
@RequestMapping("/linkong/company")
public class LkCompanyController extends BaseController
{
    private String prefix = "linkong/company";

    @Autowired
    private ILkCompanyService lkCompanyService;

    @RequiresPermissions("linkong:company:view")
    @GetMapping()
    public String company()
    {
        return prefix + "/company";
    }

    /**
     * 查询企业列表
     */
    @RequiresPermissions("linkong:company:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LkCompany lkCompany)
    {
        startPage();
        List<LkCompany> list = lkCompanyService.selectLkCompanyList(lkCompany);
        return getDataTable(list);
    }

    /**
     * 导出企业列表
     */
    @RequiresPermissions("linkong:company:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkCompany lkCompany)
    {
        List<LkCompany> list = lkCompanyService.selectLkCompanyList(lkCompany);
        ExcelUtil<LkCompany> util = new ExcelUtil<LkCompany>(LkCompany.class);
        return util.exportExcel(list, "company");
    }

    /**
     * 新增企业
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存企业
     */
    @RequiresPermissions("linkong:company:add")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LkCompany lkCompany)
    {
//        lkCompanyService.save(lkCompany);
        return toAjax(lkCompanyService.save(lkCompany));
    }

    /**
     * 修改企业
     */
    @GetMapping("/edit/{companyId}")
    public String edit(@PathVariable("companyId") String companyId, ModelMap mmap)
    {
        LkCompany lkCompany = lkCompanyService.selectLkCompanyById(companyId);
        mmap.put("lkCompany", lkCompany);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业
     */
    @RequiresPermissions("linkong:company:edit")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkCompany lkCompany)
    {
        return toAjax(lkCompanyService.updateLkCompany(lkCompany));
    }

    /**
     * 删除企业
     */
    @RequiresPermissions("linkong:company:remove")
    @Log(title = "企业", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lkCompanyService.deleteLkCompanyByIds(ids));
    }

    @PostMapping("/companyNameCheck")
    @ResponseBody
    public String companyNameCheck(String companyName){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_name", companyName);
        queryWrapper.eq("del_flag", Constants.DEL_FLAG);
        List list = lkCompanyService.list(queryWrapper);
        if(list != null && list.size() > 0){
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }
}
